package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auth: cdz
 * @Desc:
 * @Date: 2023/3/12 12:18
 */
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "购物车相关接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 功能描述: 添加购物车
     *
     * @return : com.sky.result.Result
     */
    @PostMapping("/add")
    @ApiOperation("加入购物车")
    public Result addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 功能描述: 查看购物车
     *
     * @return : com.sky.result.Result<java.util.List<com.sky.entity.ShoppingCart>>
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> list() {
        List<ShoppingCart> list = shoppingCartService.findShoppingCartsByUserId(BaseContext.getCurrentId());
        return Result.success(list);
    }

    /**
     * 功能描述: 清空
     *
     * @return : com.sky.result.Result
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result clean() {
        shoppingCartService.deleteByUserId(BaseContext.getCurrentId());
        return Result.success();
    }
}
